<template>
	<view class="case-add-container">
		<!-- 顶部导航栏 -->
		<view class="nav-bar">
			<text class="nav-title">添加案例</text>
		</view>
		
		<!-- 表单区域 -->
		<view class="form-container">
			<!-- 基本信息 -->
			<view class="form-section">
				<view class="section-title">基本信息</view>
				
				<view class="form-item">
					<text class="label">案例标题</text>
					<input type="text" 
						v-model="caseForm.title" 
						placeholder="请输入案例标题"
						class="input"
						maxlength="50"
					/>
				</view>
				
				<view class="form-item">
					<text class="label">案例价格</text>
					<input type="digit" 
						v-model="caseForm.price" 
						placeholder="请输入案例价格"
						class="input"
					/>
				</view>
				
				<view class="form-item textarea-item">
					<text class="label">案例介绍</text>
					<textarea 
						v-model="caseForm.introduce" 
						placeholder="请输入案例介绍"
						class="textarea"
						maxlength="500"
					/>
					<text class="word-count">{{ caseForm.introduce.length }}/500</text>
				</view>
			</view>
			
			<!-- 图片上传 -->
			<view class="form-section">
				<view class="section-title">案例图片</view>
				<view class="image-uploader">
					<view class="image-list">
						<view class="image-item" v-for="(image, index) in caseForm.imageList" :key="index">
							<image :src="image" mode="aspectFill" class="preview-image"></image>
							<view class="delete-btn" @click="removeImage(index)">×</view>
						</view>
						
						<view class="upload-btn" @click="chooseImage" v-if="caseForm.imageList.length < 9">
							<text class="upload-icon">+</text>
							<text class="upload-text">上传图片</text>
						</view>
					</view>
					<text class="tips">最多可上传9张图片，建议尺寸800×600px</text>
				</view>
			</view>
		</view>
		
		<!-- 提交按钮 -->
		<view class="button-container">
			<button class="submit-btn" @click="submitCase" :disabled="isSubmitting">{{ isSubmitting ? '提交中...' : '提交案例' }}</button>
		</view>
	</view>
</template>

<script setup>
import { ref, computed } from 'vue'
import request from '@/utils/request.js'

// 表单数据
const caseForm = ref({
	title: '',
	price: '',
	introduce: '',
	imageList: []
})

// 提交状态
const isSubmitting = ref(false)

// 选择图片
const chooseImage = () => {
	uni.chooseImage({
		count: 9 - caseForm.value.imageList.length,
		sizeType: ['original', 'compressed'],
		sourceType: ['album', 'camera'],
		success: (res) => {
			// 上传选中的图片
			const tempFilePaths = res.tempFilePaths
			tempFilePaths.forEach(path => {
				uploadImage(path)
			})
		}
	})
}

// 上传图片
const uploadImage = (filePath) => {
	uni.showLoading({
		title: '上传中...'
	})
	
	// 获取服务器基础URL
    const baseUrl = uni.getStorageSync('baseUrl') || 'http://127.0.0.1:8080'
    const uploadUrl = `${baseUrl}/system/uploadImg`
	
	uni.uploadFile({
		url: uploadUrl,
		filePath: filePath,
		name: 'file',
		header: {
			'sa-token': uni.getStorageSync('sa-token')
		},
		success: (uploadRes) => {
			try {
				const result = JSON.parse(uploadRes.data)
				if (result.code === 200) {
					// 添加上传成功的图片URL到列表
					caseForm.value.imageList.push(result.data)
				} else {
					uni.showToast({
						title: result.msg || '上传失败',
						icon: 'none'
					})
				}
			} catch (e) {
				uni.showToast({
					title: '上传失败，请重试',
					icon: 'none'
				})
			}
		},
		fail: (error) => {
			console.error('上传失败:', error)
			uni.showToast({
				title: '上传失败，请重试',
				icon: 'none'
			})
		},
		complete: () => {
			uni.hideLoading()
		}
	})
}

// 移除图片
const removeImage = (index) => {
	const imageUrl = caseForm.value.imageList[index]
	
	// 调用删除图片接口
	uni.showLoading({
		title: '删除中...'
	})
	
	request('/system/deleteImg', {
		method: 'POST',
		data: imageUrl
	}).then(res => {
		if (res.code === 200) {
			// 从列表中删除
			caseForm.value.imageList.splice(index, 1)
			uni.showToast({
				title: '删除成功',
				icon: 'success'
			})
		} else {
			uni.showToast({
				title: res.msg || '删除失败',
				icon: 'none'
			})
		}
	}).catch(error => {
		console.error('删除图片失败', error)
		uni.showToast({
			title: '删除失败，请重试',
			icon: 'none'
		})
	}).finally(() => {
		uni.hideLoading()
	})
}

// 提交案例
const submitCase = async () => {
	// 表单验证
	if (!caseForm.value.title.trim()) {
		uni.showToast({
			title: '请输入案例标题',
			icon: 'none'
		})
		return
	}
	
	if (!caseForm.value.price || isNaN(Number(caseForm.value.price))) {
		uni.showToast({
			title: '请输入有效的案例价格',
			icon: 'none'
		})
		return
	}
	
	if (!caseForm.value.introduce.trim()) {
		uni.showToast({
			title: '请输入案例介绍',
			icon: 'none'
		})
		return
	}
	
	if (caseForm.value.imageList.length === 0) {
		uni.showToast({
			title: '请至少上传一张案例图片',
			icon: 'none'
		})
		return
	}
	
	// 开始提交
	isSubmitting.value = true
	
	try {
		const res = await request('/admin/case/add', {
			method: 'POST',
			data: {
				title: caseForm.value.title,
				price: Number(caseForm.value.price),
				introduce: caseForm.value.introduce,
				imageList: caseForm.value.imageList
			}
		})
		
		if (res.code === 200) {
			uni.showToast({
				title: '添加成功',
				icon: 'success'
			})
			
			// 重置表单
			caseForm.value = {
				title: '',
				price: '',
				introduce: '',
				imageList: []
			}
			
			// 返回上一页
			setTimeout(() => {
				uni.navigateBack()
			}, 1500)
		} else {
			uni.showToast({
				title: res.msg || '添加失败',
				icon: 'none'
			})
		}
	} catch (error) {
		console.error('添加案例失败', error)
		uni.showToast({
			title: '网络错误，请稍后重试',
			icon: 'none'
		})
	} finally {
		isSubmitting.value = false
	}
}
</script>

<style lang="scss" scoped>
.case-add-container {
	min-height: 100vh;
	background: linear-gradient(135deg, #18122B 0%, #120C1C 100%);
	padding-bottom: 40rpx;
}

/* 导航栏样式 */
.nav-bar {
	background: linear-gradient(135deg, #221A36 0%, #18122B 100%);
	padding: 20rpx 30rpx;
	border-bottom: 1rpx solid #2D1457;
}

.nav-title {
	font-size: 36rpx;
	font-weight: bold;
	color: #A259FF;
}

/* 表单容器 */
.form-container {
	padding: 20rpx 30rpx;
}

.form-section {
	background: linear-gradient(135deg, #221A36 0%, #18122B 100%);
	margin-top: 20rpx;
	padding: 30rpx;
	border-radius: 16rpx;
	box-shadow: 0 4rpx 16rpx #7B2FF244;
	border: 1px solid #2D1457;
}

.section-title {
	font-size: 32rpx;
	font-weight: bold;
	color: #A259FF;
	margin-bottom: 30rpx;
}

/* 表单项样式 */
.form-item {
	margin-bottom: 30rpx;
}

.label {
	display: block;
	font-size: 28rpx;
	color: #B8B8D1;
	margin-bottom: 16rpx;
}

.input {
	width: 100%;
	height: 80rpx;
	background: rgba(45, 20, 87, 0.3);
	border: 1px solid #2D1457;
	border-radius: 12rpx;
	padding: 0 20rpx;
	font-size: 28rpx;
	color: #fff;
}

.textarea-item {
	position: relative;
}

.textarea {
	width: 100%;
	height: 240rpx;
	background: rgba(45, 20, 87, 0.3);
	border: 1px solid #2D1457;
	border-radius: 12rpx;
	padding: 20rpx;
	font-size: 28rpx;
	color: #fff;
}

.word-count {
	position: absolute;
	bottom: 20rpx;
	right: 20rpx;
	font-size: 24rpx;
	color: #B8B8D1;
}

/* 图片上传区域 */
.image-uploader {
	margin-top: 20rpx;
}

.image-list {
	display: flex;
	flex-wrap: wrap;
	gap: 20rpx;
}

.image-item {
	position: relative;
	width: 200rpx;
	height: 200rpx;
	border-radius: 12rpx;
	overflow: hidden;
	border: 1px solid #2D1457;
}

.preview-image {
	width: 100%;
	height: 100%;
	object-fit: cover;
}

.delete-btn {
	position: absolute;
	top: 10rpx;
	right: 10rpx;
	width: 44rpx;
	height: 44rpx;
	background: rgba(0, 0, 0, 0.6);
	color: #fff;
	border-radius: 22rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	font-size: 32rpx;
}

.upload-btn {
	width: 200rpx;
	height: 200rpx;
	background: rgba(45, 20, 87, 0.3);
	border: 1px dashed #7B2FF2;
	border-radius: 12rpx;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;
	cursor: pointer;
}

.upload-icon {
	font-size: 48rpx;
	color: #7B2FF2;
	margin-bottom: 10rpx;
}

.upload-text {
	font-size: 24rpx;
	color: #B8B8D1;
}

.tips {
	font-size: 24rpx;
	color: #B8B8D1;
	margin-top: 20rpx;
}

/* 按钮样式 */
.button-container {
	padding: 40rpx 30rpx;
}

.submit-btn {
	background: linear-gradient(45deg, #7B2FF2, #10AEFF);
	color: #fff;
	font-size: 32rpx;
	padding: 20rpx 0;
	border-radius: 12rpx;
	text-align: center;
	border: none;
	box-shadow: 0 4rpx 16rpx #7B2FF244;
}

.submit-btn:active {
	opacity: 0.8;
}

.submit-btn[disabled] {
	background: #2D1457;
	opacity: 0.7;
}
</style>